$(function () {
    get_my_shop_address(0);
});

function get_my_shop_address(page) {
    var token = $("#token").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    var search_value = $("#search_value").val();
    var jsonstr = "{token:" + token + "}";
    var url = "/except/systemcontroller/getmyaddress.action";
    $.ajax({
        url: url,
        type: 'post',
        dataType: "json",
        data: {
            page: page,
            json: jsonstr,
            searchname: search_value
        },
        async: false,
        success: function (data) {
            var code = data.code;
            var message = data.message;
            var addressinfo = "";
            if (code == 10001 || code == 10002)
                location.href = "/except/login.jsp";
            else if (code == 20000) {
                var datas = data.data;
                $(datas).each(function () {
                    addressinfo += '<div class="address_details" id="address' + this.address_id + '">' +
                        '<ul class="ware-address clearboth">' +
                        '<li onclick="shop_address(\'' + this.address_id + '\',\'' + this.name + '\',\'' + this.phone + '\',\'' + this.address + '\');"><span class="span_type">' + this.name + '</span><i>' + this.phone + '</i></li>' +
                        '<li onclick="shop_address(\'' + this.address_id + '\',\'' + this.name + '\',\'' + this.phone + '\',\'' + this.address + '\');">' +
                        '<div class="lis" onclick="chose_radio(this, \'' + this.address_id + '\', true)">' +
                        '<input type="radio"  class = "radio_types" style="margin-top: -10px;" id="' + this.address_id + '">' +
                        '<span>' + this.address + '</span>' +
                        '</div>' +
                        '<div class="clearboth"></div>' +
                        '</li>' +
                        '<li class="li3">' +
                        '<span onclick="edit_address(\'' + this.address_id + '\');" class="buttons">编辑</span>' +
                        '<span onclick="rem_address(\'' + this.address_id + '\');" class="buttons">删除</span>' +
                        '</li>' +
                        '</ul>' +
                        '</div>';
                });
                remove_advertising()
            } else if (code == 20013) {
            } else {
                $("#hint_message").html(message);
                $("#hintbg").show();
            }
            if (page == 0)
                $("#address_list").empty();
            $("#address_list").append(addressinfo);
            $("#page").val(page * 1 + 1);
        },
        error: function () {
            $("#hint_message").html("网络异常");
            $("#hintbg").show();
        }
    });
}

function shop_address(addressid, name, phone, address) {
    var upurl = $("#upurl").val();
    var shoptype = $("#shoptype").val();
    if (!isempty(upurl) && isempty(shoptype)) {
        $("#addressid").val(addressid);
        $("#addressname").val(name);
        $("#address").val(address);
        $("#addressphone").val(phone);
        $("#address_frm").attr("action", upurl).submit();
    }
}

$(window).scroll(function () {
    if ($(document).scrollTop() <= 0) {
        get_my_shop_address(0);
    }
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        var page = $("#page").val();
        get_my_shop_address(page);
    }
});

function searchinfo() {
    var search_value = $("#search_value").val();
    get_my_shop_address(0);
}

function chose_radio(radio, addressid, flag) {
    $("#" + addressid).attr("checked", flag);
    if (flag) {
        $(radio).attr("onclick", "chose_radio(this,'" + addressid + "', false);")
    } else
        $(radio).attr("onclick", "chose_radio(this,'" + addressid + "', true);");
}

function choseAll(radio, flag) {
    $("#address_list :radio").attr("checked", flag);
    $(this).attr("checked", flag);
    if(flag) {
        $("#address_list :radio").each(function () {
            $(this).attr("onclick", "chose_radio('" + $(this).attr("id") + "', false);")
        });
    } else {
        $("#address_list :radio").each(function () {
            $(this).attr("onclick", "chose_radio('" + $(this).attr("id") + "', true);")
        });
    }
}

function forword_chose_shop() {
    var flag = false;
    var addressid = "";
    $("#address_list :radio").each(function () {
        var checked = $(this).attr("checked");
        if (checked) {
            flag = true;
            var id = $(this).attr("id");
            addressid += id + ",";
        }
    });
    if(flag) {
        addressid = "[" + addressid.substr(0, addressid.length - 1) + "]";
        location.href = "/except/files/shop/chose_reptory_shop.jsp?addressid=" + addressid;
    } else {
        $("#hint_message").html("请选择收货地址");
        $("#hintbg").show();
    }
}

function edit_address(addressid) {
    location.href = "/except/files/user/add_new_address.jsp?addressid=" + addressid;
}

function rem_address(addressid) {
    $("#addressid").val(addressid)
    $("#message").html("确定删除吗？");
    $("#confirmbg").show();
}

function confirm_state(state) {
    $("#confirmbg").hide();
    var token = $("#token").val();
    var addressid = $("#addressid").val();
    if (isempty(token)) {
        location.href = "/except/login.jsp";
        return;
    }
    if (state == 1) {
        var jsonstr = "{token:" + token + "}";
        var url = "/except/systemcontroller/deladdress.action";
        $.ajax({
            url: url,
            type: 'post',
            dataType: "json",
            data: {
                addressid: addressid,
                json: jsonstr,
            },
            async: false,
            success: function (data) {
                var code = data.code;
                var message = data.message;
                var addressinfo = "";
                if (code == 10001 || code == 10002)
                    location.href = "/except/login.jsp";
                else if (code == 20000) {
                    $("#hint_message").html("删除成功");
                    $("#hintbg").show();
                    $("#address" + addressid).remove();
                } else {
                    $("#hint_message").html(message);
                    $("#hintbg").show();
                }
            }
        });
    }
}